﻿using Apps.BLL.HC;
using Apps.Common;
using Apps.Locale;
using Apps.Models;
using Apps.Web.Core;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web.Mvc;

namespace Apps.Web.Areas.HC.Controllers
{
    public class HC_ITEMSController : BaseController
    {
        HC_ITEMS_BLL bll = new HC_ITEMS_BLL();
        HC_EXTEND_FIELD_BLL fieldBll = new HC_EXTEND_FIELD_BLL();
        HC_ITEM_EXTEND_REL_BLL relBll = new HC_ITEM_EXTEND_REL_BLL();
        ValidationErrors errors = new ValidationErrors();

        [SupportFilter]
        public ActionResult Index()
        {
            return View();
        }
        [HttpPost]
        [SupportFilter(ActionName = "Index")]
        public JsonResult GetList()
        {
            int totalCount = 0;
            string queryStr = Utils.ObjectToStr(Request["queryStr"]);
            int pageSize = Utils.ObjToInt(Request["rows"], 15);
            int pageIndex = Utils.ObjToInt(Request["page"], 1);
            string sort = Utils.ObjectToStr(Request["sort"]);
            string order = Utils.ObjectToStr(Request["order"]);
            List<HC_ITEMS> list = bll.GetList(queryStr, pageIndex, pageSize, sort, order, ref totalCount);
            return Json(new Pagination.PaginationResult<HC_ITEMS>(totalCount, list.ToList()));
        }
        #region 创建
        [HttpPost]
        public JsonResult GetListDetail(string itemId)
        {
            int totalCount = 0;
            string queryStr = Utils.ObjectToStr(Request["queryStr"]);
            int pageSize = Utils.ObjToInt(Request["rows"], 15);
            int pageIndex = Utils.ObjToInt(Request["page"], 1);
            string sort = Utils.ObjectToStr(Request["sort"]);
            string order = Utils.ObjectToStr(Request["order"]);
            List<V_HC_ITEM_FIELD_VALUE> list = bll.Get_FIELD_REFERENCE_VALUE_List(itemId, pageIndex, pageSize, sort, order, ref totalCount).ToList();
            return Json(new Pagination.PaginationResult<V_HC_ITEM_FIELD_VALUE>(totalCount, list.ToList()));
        }


        [SupportFilter]
        public ActionResult Create()
        {
            //List<HC_EXTEND_FIELD> fieldList = fieldBll.GetList();
            //ViewBag.FieldList = fieldList;
            return View();
        }

        [HttpPost]
        [SupportFilter]
        public JsonResult Create(HC_ITEMS_EXTEND model)
        {
            if (model != null && ModelState.IsValid)
            {
                model.ID = ResultHelper.NewId;

                Regex regex = new Regex("\\s+", RegexOptions.IgnoreCase);
                model.ITEM_NAME = regex.Replace(model.ITEM_NAME, "");
                model.ITEM_TITLE = regex.Replace(model.ITEM_TITLE, "");

                if (string.IsNullOrEmpty(model.ITEM_NAME))
                {
                    return Json(JsonHandler.CreateMessage(0, "化产项目别名不能为空!"));
                }
                bool isTeshu = Regex.Replace(model.ITEM_NAME, "[\\u4e00-\\u9FA5A-Za-z0-9]", "", RegexOptions.IgnoreCase).Length > 0;
                if(isTeshu)
                {
                    return Json(JsonHandler.CreateMessage(0, "化产项目别名不能包含特殊名称!"));
                }
                if (string.IsNullOrEmpty(model.ITEM_TITLE))
                {
                    return Json(JsonHandler.CreateMessage(0, "化产项目名称不能为空!"));
                }
                if (bll.AddIsRepeated(model.ITEM_NAME))
                {
                    return Json(JsonHandler.CreateMessage(0, "已存在相同化产项目别名!"));
                }
                if (bll.AddIsRepeated(model.ITEM_TITLE))
                {
                    return Json(JsonHandler.CreateMessage(0, "已存在相同化产项目名称!"));
                }
                string jsonstr = Utils.ToJson(model, Formatting.Indented);
                if (bll.DoAdd(model))
                {
                    LogHandler.WriteServiceLog(GetUserId(), jsonstr, "成功", "创建", "HC_HC_ITEMS");
                    return Json(JsonHandler.CreateMessage(1, Resource.InsertSucceed));
                }
                else
                {
                    string ErrorCol = errors.Error;
                    LogHandler.WriteServiceLog(GetUserId(), jsonstr, "失败", "创建", "HC_HC_ITEMS");
                    return Json(JsonHandler.CreateMessage(0, Resource.InsertFail + ErrorCol));
                }
            }
            else
            {
                return Json(JsonHandler.CreateMessage(0, Resource.InsertFail));
            }
        }
        #endregion

        #region 修改
        [SupportFilter]
        public ActionResult Edit(string id)
        {
            HC_ITEMS_EXTEND entity = bll.Get_ITEMS_EXTEND(id);
            ViewData.Model = entity;
            return View(entity);
        }

        [HttpPost]
        [SupportFilter]
        public JsonResult Edit(HC_ITEMS_EXTEND model)
        {
            if (model != null && ModelState.IsValid)
            {
                if (string.IsNullOrEmpty(model.ITEM_NAME))
                {
                    return Json(JsonHandler.CreateMessage(0, "化产项目别名不能为空!"));
                }
                if (bll.EditIsRepeated(model.ID, model.ITEM_NAME))
                {
                    return Json(JsonHandler.CreateMessage(0, "已存在相同化产项目别名!"));
                }
                bool isTeshu = Regex.Replace(model.ITEM_NAME, "[\\u4e00-\\u9FA5A-Za-z0-9]", "", RegexOptions.IgnoreCase).Length > 0;
                if (isTeshu)
                {
                    return Json(JsonHandler.CreateMessage(0, "化产项目别名不能包含特殊名称!"));
                }
                //if (model.FIELDS.Length > 0)
                //{
                //    List<HC_ITEM_EXTEND_REL> relList = new List<HC_ITEM_EXTEND_REL>();
                //    foreach (var item in model.FIELDS.Split(','))
                //    {
                //        relList.Add(new HC_ITEM_EXTEND_REL()
                //        {
                //            ID = ResultHelper.NewId,
                //            EXTEND_FIELD_ID = item,
                //            ITEM_ID = model.ID
                //        });
                //    }
                //    model.fieldList = relList;
                //}
                string jsonstr = Utils.ToJson(model, Formatting.Indented);
                if (bll.DoEdit(model))
                {
                    LogHandler.WriteServiceLog(GetUserId(), jsonstr, "成功", "修改", "HC_HC_ITEMS");
                    return Json(JsonHandler.CreateMessage(1, Resource.EditSucceed));
                }
                else
                {
                    string ErrorCol = errors.Error;
                    LogHandler.WriteServiceLog(GetUserId(), jsonstr, "失败", "修改", "HC_HC_ITEMS");
                    return Json(JsonHandler.CreateMessage(0, Resource.EditFail + ErrorCol));
                }
            }
            else
            {
                return Json(JsonHandler.CreateMessage(0, Resource.EditFail));
            }
        }
        #endregion

        #region 删除
        [HttpPost]
        [SupportFilter]
        public ActionResult Delete(string id)
        {
            if (!string.IsNullOrWhiteSpace(id))
            {
                if (relBll.IsHaveRel(id))
                {
                    string ErrorCol = "【该扩展列已被使用，禁止删除】";
                    LogHandler.WriteServiceLog(GetUserId(), "ID" + id + "," + ErrorCol, "失败", "删除", "HC_HC_ITEMS");
                    return Json(JsonHandler.CreateMessage(0, Resource.DeleteFail + ErrorCol));
                }
                else
                {
                    if (bll.DoDelete(id))
                    {
                        LogHandler.WriteServiceLog(GetUserId(), "ID:" + id, "成功", "删除", "PUB_SUPPLIER");
                        return Json(JsonHandler.CreateMessage(1, Resource.DeleteSucceed));
                    }
                    else
                    {
                        string ErrorCol = "【删除失败】";
                        LogHandler.WriteServiceLog(GetUserId(), "ID" + id + "," + ErrorCol, "失败", "删除", "HC_HC_ITEMS");
                        return Json(JsonHandler.CreateMessage(0, Resource.DeleteFail + ErrorCol));
                    }
                }
            }
            else
            {
                return Json(JsonHandler.CreateMessage(0, Resource.DeleteFail));
            }
        }
        #endregion
    }
}